package com.example.shoppings.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shoppings.R;
import com.example.shoppings.sql.Buyer;
import com.example.shoppings.sql.Orders;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hasee on 2017-05-10.
 */

public class PayActivity extends Activity {

    @BindView(R.id.tl_custom)
    Toolbar tlCustom;
    @BindView(R.id.iv_product)
    ImageView ivProduct;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.edt_name)
    EditText edtName;
    @BindView(R.id.edt_phone)
    EditText edtPhone;
    @BindView(R.id.edt_address)
    EditText edtAddress;
    @BindView(R.id.tv_allprice)
    TextView tvAllprice;
    @BindView(R.id.tv_pay)
    TextView tvPay;

    private long orderId;
    private Orders orders;

    private String name,phone,address;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        ButterKnife.bind(this);
        orderId=getIntent().getLongExtra("orderId",0);
        orders= (DataSupport.where("orderId= ?",orderId+"").find(Orders.class)).get(0);
        setData();
    }

    private void setData(){
        ivProduct.setImageResource(orders.getProducts().getImgMainPath().get(0));
        tvName.setText(orders.getProducts().getProName());
        tvNum.setText("x "+orders.getNum());
        tvPrice.setText("￥："+orders.getOrderPrice());
    }

    private void commitOrder(){
        name=edtName.getText().toString();
        phone=edtPhone.getText().toString();
        address=edtAddress.getText().toString();
        if(name.equals("") || phone.equals("") || address.equals("")){
            Toast.makeText(this,"请填写完整信息！",Toast.LENGTH_SHORT).show();
            return;
        }
        Buyer buyer=new Buyer();
        buyer.setName(name);
        buyer.setPhone(phone);
        buyer.setAddress(address);
        orders.setBuyer(buyer);
        orders.save();
    }

    @OnClick(R.id.tv_pay)
    public void onClick() {
        commitOrder();
    }
}
